<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <!--标准mui.css-->
	<link rel="stylesheet" type="text/css" href="../css/mui/mui.min.css" />
    <style type="text/css">
        .aui-list-item.wechat-top {
            background-color: #f3f3f7;
        }
        .aui-list .wechat-avatar {
            width:3rem;
        }
        .wechat-avatar .aui-row-padded {
            background-color: #dddee0;
            padding: 0.1rem;
            border-radius: 0.2rem;
            margin-left: -0.05rem;
            margin-right: -0.05rem;
        }
        .wechat-avatar .aui-row-padded [class*=aui-col-xs-] {
            padding: 0.05rem;
        }
        .wechat-avatar,
        .wechat-avatar > img {
            border-radius:0.2rem;
        }
    </style>
</head>
<body>
    <div class="aui-content aui-margin-b-15">
        <ul class="aui-list aui-media-list aui-margin-t-10" id="OA_task_1">
            <!--<li class="aui-list-item wechat-top">
                <div class="aui-media-list-item-inner">
                    <div class="aui-list-item-label-icon">
                        <i class="aui-iconfont aui-icon-mobile"></i>
                    </div>
                    <div class="aui-list-item-inner">
                        Iphone 微信已登录
                    </div>
                </div>
            </li>
            <li class="aui-list-item aui-list-item-middle wechat-top">
                <div class="aui-media-list-item-inner">
                    <div class="aui-list-item-media wechat-avatar">
                        <div class="aui-badge">9</div>
                        <div class="aui-row-padded">
                            <div class="aui-col-xs-4">
                                <img src="../image/demo1.png" />
                            </div>
                            <div class="aui-col-xs-4">
                                <img src="../image/demo2.png" />
                            </div>
                            <div class="aui-col-xs-4">
                                <img src="../image/demo3.png" />
                            </div>
                            <div class="aui-col-xs-4">
                                <img src="../image/demo4.png" />
                            </div>
                            <div class="aui-col-xs-4">
                                <img src="../image/demo5.png" />
                            </div>
                            <div class="aui-col-xs-4">
                                <img src="../image/demo6.png" />
                            </div>
                            <div class="aui-col-xs-4">
                                <img src="../image/demo1.png" />
                            </div>
                            <div class="aui-col-xs-4">
                                <img src="../image/demo3.png" />
                            </div>
                            <div class="aui-col-xs-4">
                                <img src="../image/demo5.png" />
                            </div>
                        </div>
                    </div>
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-text">
                            <div class="aui-list-item-title">AUI微信开发交流群</div>
                            <div class="aui-list-item-right">08:00</div>
                        </div>
                        <div class="aui-list-item-text aui-font-size-12">
                            流浪男:欢迎使用AUI 2.0
                        </div>
                    </div>
                </div>
            </li>
            <li class="aui-list-item aui-list-item-middle wechat-top">
                <div class="aui-media-list-item-inner">
                    <div class="aui-list-item-media wechat-avatar">
                        <div class="aui-row-padded">
                            <div class="aui-col-xs-6">
                                <img src="../image/demo2.png" />
                            </div>
                            <div class="aui-col-xs-6">
                                <img src="../image/demo3.png" />
                            </div>
                            <div class="aui-col-xs-6">
                                <img src="../image/demo1.png" />
                            </div>
                            <div class="aui-col-xs-6">
                                <img src="../image/demo5.png" />
                            </div>
                        </div>

                    </div>
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-text">
                            <div class="aui-list-item-title">AUI 2.0讨论组</div>
                            <div class="aui-list-item-right">08:00</div>
                        </div>
                        <div class="aui-list-item-text aui-font-size-12">
                            AUI:官方网站 www.auicss.com
                        </div>
                    </div>
                </div>
            </li>
            <li class="aui-list-item aui-list-item-middle">
                <div class="aui-media-list-item-inner">
                    <div class="aui-list-item-media wechat-avatar">
                        <div class="aui-badge">9</div>
                        <img src="../image/demo5.png" />
                    </div>
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-text">
                            <div class="aui-list-item-title">流浪男</div>
                            <div class="aui-list-item-right">星期一</div>
                        </div>
                        <div class="aui-list-item-text aui-font-size-12">
                            Hello AUI 2.0!
                        </div>
                    </div>
                </div>
            </li>
            <li class="aui-list-item  mui-table-view-cell">
                <div class="mui-slider-right mui-disabled">
                    <a class="mui-btn aui-btn-danger">删除</a>
                </div>
                <div class="aui-media-list-item-inner mui-slider-handle">
                    <div class="aui-list-item-media wechat-avatar">
                        <img src="../image/demo1.png" />
                    </div>
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-text">
                            <div class="aui-list-item-title">张三</div>
                            <div class="aui-list-item-right">1天前</div>
                        </div>
                        <div class="aui-list-item-text aui-font-size-12">
                            Hello AUI 2.0!
                        </div>
                    </div>
                </div>
            </li>
            <li class="aui-list-item aui-list-item-middle" onclick="test()">
                <div class="aui-media-list-item-inner">
                    <div class="aui-list-item-media wechat-avatar">
                        <div class="aui-dot"></div>
                        <img src="../image/demo4.png" />
                    </div>
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-text">
                            <div class="aui-list-item-title">李四</div>
                            <div class="aui-list-item-right">星期一</div>
                        </div>
                        <div class="aui-list-item-text aui-font-size-12">
                            Hello AUI 2.0!
                        </div>
                    </div>
                </div>
            </li>-->
        </ul>
    </div>
</body>
<script type="text/javascript" src="../script/zepto.min.js" ></script>
<script type="text/javascript" src="../script/api.js" ></script>
<script type="text/javascript" src="../script/mui/mui.min.js"></script>
<script type="text/javascript">
mui.init();
			(function($) {
				//第一个demo，拖拽后显示操作图标，点击操作图标删除元素；
				$('#OA_task_1').on('tap', '.mui-btn', function(event) {
  					var elem = this;
  					var li = elem.parentNode.parentNode;
            var targetId = li.getAttribute("data-name");
            var conversationType = li.getAttribute("data-type");
            var a_type = elem.getAttribute("a-type");
            if(a_type==0){
                mui.confirm('确认删除该条记录？', '', btnArray, function(e) {
                  if (e.index == 0) {
                    api.sendEvent({
                        name:'removeConversation',
                        extra:{
                            targetId:targetId,
                            conversationType:conversationType
                        }
                    })
                    li.parentNode.removeChild(li);
                  } else {
                    setTimeout(function() {
                      $.swipeoutClose(li);
                    }, 0);
                  }
                });
            }else {
                clearMessagesUnreadStatus(conversationType,targetId);
            }
				});
        var btnArray = ['确认', '取消'];
			})(mui);
</script>
<script>
/*rong全局*/
var rong;
/*获取会话消息列表*/
function coversationList(){
    //消息列表
    rong.getConversationList(function (ret, err) {
        //alert(JSON.stringify(ret));
        if(ret.status=='success'){
              api.ajax({
                url: ServeUrl + "m=Index&a=getChatList",
                method: 'post',
                data: {
                    values: {
                        jsonStr: ret.result
                    }
                }
            }, function(ret, err) {
                if (ret.status==1) {
                    //alert(ret.data);
                    ret.data = $.parseJSON(ret.data);
                    //alert(JSON.stringify(ret.data));
                    var username = $api.getStorage('username');

                    var html = '';
                    for (var i = 0; i < ret.data.length; i++) {
                          if(ret.data[i].objectName=='RC:ImgMsg'){
                              ret.data[i].latestMessage.text = "[图片]";
                          }else if (ret.data[i].objectName=='RC:VcMsg') {
                              ret.data[i].latestMessage.text = "[语音]";
                          }else if (ret.data[i].objectName=='RC:DizNtf') {
                              ret.data[i].latestMessage.text = "[系统消息]";
                          }
                          if(ret.data[i].conversationType=="PRIVATE"){
                              html+='<li class="aui-list-item  mui-table-view-cell" data-name="'+ret.data[i].targetId+'" data-type="'+ret.data[i].conversationType+'"  onclick="openWin('+"'"+ret.data[i].targetId+"','"+ret.data[i].cn_name+"','"+ret.data[i].conversationType+"','"+ret.data[i].latestMessageId+"','"+ret.data[i].user_img+"',"+ret.data[i].sentTime+')">'
                          }else{
                              html+='<li class="aui-list-item  mui-table-view-cell" data-name="'+ret.data[i].targetId+'" data-type="'+ret.data[i].conversationType+'"  onclick="openWin('+"'"+ret.data[i].targetId+"','"+ret.data[i].cn_name+"','"+ret.data[i].conversationType+"','"+ret.data[i].latestMessageId+"','"+ret.data[i].user_img+"',"+ret.data[i].sentTime+')">'
                          }
                          html+='<div class="mui-slider-right mui-disabled">'
                          html+='<a class="mui-btn aui-btn-warning" a-type="1">标记已读</a>'
                          html+='<a class="mui-btn aui-btn-danger" a-type="0">删除</a>'
                          html+='</div>'
                          html+='<div class="aui-media-list-item-inner mui-slider-handle">'
                          html+='<div class="aui-list-item-media wechat-avatar">'
                          html+='<div class="aui-badge" id="id_'+ret.data[i].targetId+'"></div>'
                          if(ret.data[i].conversationType=="PRIVATE"){
                              html+='<img src="'+ServeIp+'include/data/headImg/'+ret.data[i].user_img+'" />'
                          }else{
                              if(ret.data[i].user_img==null||ret.data[i].user_img==""){
                                  html+='<img src="../image/users.png" />'
                              }else {
                                  html+='<img src="'+ServeIp+'include/data/headImg/'+ret.data[i].user_img+'" />'
                              }
                          }
                          html+='</div>'
                          html+='<div class="aui-list-item-inner">'
                          html+='<div class="aui-list-item-text">'
                          if(ret.data[i].conversationType=="PRIVATE"){
                              html+='<div class="aui-list-item-title">'+ret.data[i].cn_name+'</div>'
                          }else{
                              html+='<div class="aui-list-item-title">'+ret.data[i].cn_name+'</div>'
                          }
                          html+='<div class="aui-list-item-right">'+sj(ret.data[i].sentTime)+'</div>'
                          html+='</div>'
                          html+='<div class="aui-list-item-text aui-font-size-12">'+ret.data[i].latestMessage.text+'</div>'
                          html+='</div>'
                          html+='</div>'
                          html+='</li>';
                          getUnreadCount(ret.data[i].conversationType,ret.data[i].targetId);
                    }
                    $("#OA_task_1").html(html);
                } else {
                    api.toast({
                        msg:'获取失败'
                    });
                }
            });
            getTotalUnreadCount();
        }
    })
}

//清除会话未读消息条数
function clearMessagesUnreadStatus(conversationType,targetId){
    rong.clearMessagesUnreadStatus({
      conversationType: conversationType,
      targetId: targetId
    }, function(ret, err) {
      //alert(ret.status);
      coversationList();
    })
}
//获取会话未读消息条数
function getUnreadCount(conversationType,targetId){
    rong.getUnreadCount({
        conversationType: conversationType,
        targetId: targetId
    }, function(ret, err) {
        if(ret.result>0){
            $("#id_"+targetId).html(ret.result);
            api.setAppIconBadge({
                badge: ret.result
            });
        }else {
            $("#id_"+targetId).hide();
        }
    })
}
/*
＊监听新消息
＊当有新消息传来时，利用sendEvent发出一个事件，同时传递消息内容，可以在会话页面进行一次监听接收
*/
function receiveMessageListener(){
    rong.setOnReceiveMessageListener(function (ret, err) {
        //alert(JSON.stringify(ret));
        if(ret.result){
            //alert(JSON.stringify(ret.result.message));
            coversationList();
            //发送事件
            api.sendEvent({
                name:'getNewMessage',
                extra:{
                    data:ret.result.message
                }
            })
        }
    })
}

/*
*移除消息列表
＊注意要放在消息列表页，不要放在会话页面
＊在会话页面利用sendEvent发出一个发送消息的事件，在消息列表页监听
*/
function removeConversation(targetId,conversationType){
    rong.removeConversation({
        conversationType: conversationType,
        targetId: targetId
    }, function(ret, err) {
      api.toast({
          msg:'移除成功'
      });
    })

}

/*
*获取消息历史
＊注意要放在消息列表页，不要放在会话页面
＊在消息列表页利用sendEvent发出一个发送消息的事件，在会话页面监听
*/
function getHistoryMessages(targetId,conversationType,latestMessageId){
    rong.getHistoryMessages({
        conversationType: conversationType,
        targetId: targetId,
        oldestMessageId: parseInt(latestMessageId)+1,
        count: 20
    }, function(ret, err) {
        if(ret.status=="success"){
            //alert(JSON.stringify(ret.result));
            api.ajax({
                url: ServeUrl + "m=Index&a=getChatHistory",
                method: 'post',
                data: {
                    values: {
                        jsonStr: ret.result
                    }
                }
            }, function(ret, err) {
                  if(ret.status==1){
                      ret.data = $.parseJSON(ret.data);
                      //alert(JSON.stringify(ret.data));
                      $api.setStorage('historyMessages', ret.data);
                  }
            });

            //alert("发送事件");
        }else{
            api.toast({
                msg:'获取失败'
            });
        }

    })
}

/*
*获取最后一条消息历史
＊注意要放在消息列表页，不要放在会话页面
＊在消息列表页利用sendEvent发出一个发送消息的事件，在会话页面监听
*/
function getLatestMessages(targetId,conversationType,count){
    rong.getLatestMessages({
        conversationType: conversationType,
        targetId: targetId,
        count: count
    }, function(ret, err) {
        //alert(JSON.stringify(ret));
        //最后一条消息的发送事件，在个人页监听
        api.sendEvent({
            name:'lastMsg',
            extra:{
                data:ret.result
            }
        });
    })
}

//把消息记录加到本地数据库
function addMessage(jsonStr){
    api.ajax({
        url: ServeUrl + 'm=Index&a=addMessage',
        method: 'post',
        cache : false,
            timeout : 30,
            dataType : 'json',
        data: {
            values: {
                jsonStr : jsonStr
            }
        }
    },function(ret, err){


    });
}

//把群组ID加到本地数据库
function addGroup(groupID,groupName,usersId){
    //alert(groupID);
    var username = $api.getStorage('username');
    var ryId = $api.getStorage('ryId');

    api.ajax({
        url: ServeUrl + 'm=Index&a=addGroup',
        method: 'post',
        cache : false,
            timeout : 30,
            dataType : 'json',
        data: {
            values: {
                groupID : groupID,
                groupName:groupName,
                usersId:usersId,
                username:ryId
            }
        }
    },function(ret, err){
        coversationList();

    });
}

//更新群组
function updateGroup(targetId,val){
    api.ajax({
        url: ServeUrl + "m=Index&a=updateGroupInfo",
        method: 'post',
        data: {
            values: {
                val: val,
                targetId:targetId
            }
        }
    }, function(ret, err) {
        //alert("chenggong!");
    });
}


/*
*发送消息的函数
＊注意要放在消息列表页，不要放在会话页面
＊在会话页面利用sendEvent发出一个发送消息的事件，在消息列表页监听
*/
function sendMessage(type,targetId,content,extra,conversationType){
    rong.sendTextMessage({
            conversationType: ''+conversationType+'',
            targetId: ''+targetId+'',
            text: ''+content+'',
            extra: ''+extra+''
        }, function (ret, err) {
            if (ret.status == 'prepare'){
                //单聊准备发送，向会话页面发送正在发送消息事件
                api.sendEvent({
                    name:'insertSendMessage',
                    extra:{
                        data:content
                    }
                });

                addMessage(ret.result.message);
              //  alert(JSON.stringify(ret.result));

            }else if (ret.status == 'success'){
                //成功后处理
                coversationList();

            }else if (ret.status == 'error'){
                //失败
                api.toast({
                    msg:'发送失败'
                });
            }

        }
    );
}

/*
*发送图片的函数
＊注意要放在消息列表页，不要放在会话页面
＊在会话页面利用sendEvent发出一个发送图片的事件，在消息列表页监听
*/
function sendImageMessage(type,targetId,imagePath,conversationType){
    rong.sendImageMessage({
      conversationType: conversationType,
      targetId: targetId,
      imagePath: imagePath,
      extra: ''
    }, function(ret, err) {
        if (ret.status == 'prepare'){
            //单聊准备发送，向会话页面发送正在发送消息事件
            api.sendEvent({
                name:'insertSendImage',
                extra:{
                    data:imagePath
                }
            });
            addMessage(ret.result.message);
            //
        }else if (ret.status == 'progress'){
            //alert(ret.result.progress);
        }else if (ret.status == 'success'){
            coversationList();
        }else if (ret.status == 'error'){
            api.toast({
                msg:'发送失败'
            });
        }
    });
}

/*
*发送语音的函数
＊注意要放在消息列表页，不要放在会话页面
＊在会话页面利用sendEvent发出一个发送图片的事件，在消息列表页监听
*/
function sendVoiceMessage(type,targetId,voicePath,duration,conversationType){
    rong.sendVoiceMessage({
        conversationType: conversationType,
        targetId: targetId,
        voicePath: voicePath,
        duration: parseInt(duration),
        extra: ''
    }, function(ret, err) {
        //alert(JSON.stringify(ret));
        if (ret.status == 'prepare'){
            //单聊准备发送，向会话页面发送正在发送消息事件
            api.sendEvent({
                name:'insertSendVoice',
                extra:{
                    data:voicePath,
                    duration:parseInt(duration)

                }
            });
            addMessage(ret.result.message);
            //alert(JSON.stringify(ret.result.message));
        }else if (ret.status == 'progress'){
            //alert(ret.result.progress);
        }else if (ret.status == 'success'){
            coversationList();
        }else if (ret.status == 'error'){
            api.toast({
                msg:'发送失败'
            });
        }
    });
}

//创建群聊
function createDiscussion(groupName,usersId,usersStr){
    rong.createDiscussion({
      name: groupName,
      userIdList: eval(usersId)
    }, function(ret, err) {
      //alert(JSON.stringify(err));
      if (ret.status == 'success'){
          addGroup(ret.result.discussionId,groupName,usersStr);
          //alert(ret.result.discussionId);
      }else{
          //alert(JSON.stringify(err));
          api.toast({
              msg:'创建失败'
          });
      }
    })

}

//添加成员到群组中
function addMemberToDiscussion(targetId,usersIdstr,val){
    rong.addMemberToDiscussion({
        discussionId: targetId,
        userIdList: eval(usersIdstr)
    }, function(ret, err) {
        if (ret.status == 'success'){
            updateGroup(targetId,val);
        }else{
            //alert(JSON.stringify(err));
            api.toast({
                msg:'添加失败'
            });
        }
     });
}


//获取群聊信息
function getDiscussion(){
    rong.getDiscussion({
        discussionId: '93efb369-b9b5-4665-870a-13776777f781'
    }, function(ret, err) {
        alert(JSON.stringify(ret));
        if (ret.status == 'success')
            alert(JSON.stringify(ret.result.id));
        else
            api.toast({ msg: err.code });
    })
}

//设置群组名称
function setDiscussionName(discussionId,name){
    rong.setDiscussionName({
        discussionId: discussionId,
        name: name
    }, function(ret, err) {
        if (ret.status == 'success'){
            api.toast({ msg: "设置成功" });
            //alert(JSON.stringify(ret.status));
        }else{
            api.toast({ msg: err.code });
        }
    })
}

//退出群组
function quitDiscussion(discussionId){
    rong.quitDiscussion({
        discussionId: discussionId
    }, function(ret, err) {
        if (ret.status == 'success'){
            //alert(JSON.stringify(ret.status));
            api.toast({ msg: "退出成功" });
        }else{
            api.toast({ msg: err.code });
        }
    })
}

//获取所有未读数
function getTotalUnreadCount(){
    rong.getTotalUnreadCount(function(ret, err) {
        api.sendEvent({
            name:'totalUnread',
            extra:{
                count:ret.result

            }
        });
    })
}

/*断开融云*/
function disconnect(){
    rong.disconnect({
      isReceivePush: true
    }, function(ret, err) {
      if ('success' == ret.status) {
          api.toast({ msg: '断开连接成功!' });
      }
    });
}


/*
＊融云的初始化
*/
function rongCloud(){
    var token = $api.getStorage('ryToken');
    //alert(token);
    //融云初始化
    rong.init(function(ret, err){
    });
    //监听新消息
    receiveMessageListener();
    //连接
    rong.connect({
        token: token
    },function(ret, err){
        if(ret.status=='success'){
            //消息列表
            coversationList();
        }
    });
}
apiready = function(){
    rong = api.require('rongCloud2');
    rongCloud();
    coversationList();
    //监听来自会话页面发送消息的事件
    api.addEventListener({
        name:'sendMessage'
    },function(ret){
        if(ret && ret.value){
            var value = ret.value;
            sendMessage(''+value.type+'',''+value.targetId+'',''+value.content+'',''+value.extra+'',''+value.conversationType+'');
        }
    });

    //监听来自会话页面发送图片的事件
    api.addEventListener({
        name:'sendPic'
    },function(ret){
        if(ret && ret.value){
            var value = ret.value;
            sendImageMessage(''+value.type+'',''+value.targetId+'',''+value.imagePath+'',''+value.conversationType+'');
        }
    });

    //监听来自会话页面发送语音的事件
    api.addEventListener({
        name:'sendVoice'
    },function(ret){
        if(ret && ret.value){
            //alert(JSON.stringify(ret.value));
            var value = ret.value;
            sendVoiceMessage(''+value.type+'',''+value.targetId+'',''+value.voicePath+'',''+value.duration+'',''+value.conversationType+'');
        }
    });

    //监听消息列表删除
    api.addEventListener({
        name:'removeConversation'
    },function(ret){
        if(ret && ret.value){
            var value = ret.value;
            removeConversation(value.targetId,value.conversationType);
        }
    });

    //监听获取历史记录
    api.addEventListener({
        name:'getHistoryMessages'
    },function(ret){
        if(ret && ret.value){
            var value = ret.value;
            //alert(JSON.stringify(value));
            getHistoryMessages(value.targetId,value.conversationType,value.latestMessageId);
        }
    });

    //监听个人页请求的获取最后一条消息
    api.addEventListener({
        name:'getLastMsg'
    },function(ret){
        if(ret && ret.value){
            var value = ret.value;
            getLatestMessages(value.targetId,value.conversationType,value.count);
            //getHistoryMessages(value.targetId,value.conversationType,value.latestMessageId);
        }
    });

    //监听创建群组
    api.addEventListener({
        name:'createGroupChat'
    },function(ret){
        if(ret && ret.value){
            var value = ret.value;
            createDiscussion(value.groupName,value.usersId,value.usersStr);
        }
    });


    //监听获取群组信息
    api.addEventListener({
        name:'getDiscussion'
    },function(ret){
        if(ret){
            getDiscussion();
        }
    });

    //监听设置群组名称
    api.addEventListener({
        name:'setDiscussionName'
    },function(ret){
        if(ret && ret.value){
            var value = ret.value;
            setDiscussionName(value.targetId,value.groupName);
        }
    });

    //监听退出群组
    api.addEventListener({
        name:'quitDiscussion'
    },function(ret){
        if(ret && ret.value){
            var value = ret.value;
            quitDiscussion(value.targetId);
        }
    });

    //监听添加成员到群组
    api.addEventListener({
        name:'addMemberToDiscussion'
    },function(ret){
        if(ret && ret.value){
            var value = ret.value;
            //alert(JSON.stringify(value));
            addMemberToDiscussion(value.targetId,value.usersIdstr,value.val);
        }
    });


    //监听断开融云连接
    api.addEventListener({
        name:'disconnect'
    },function(ret){
        if(ret){
            disconnect();
        }
    });


    //下拉刷新
    api.setRefreshHeaderInfo({
        visible: true,
        loadingImg: 'widget://image/refresh.png',
        bgColor: '#ccc',
        textColor: '#fff',
        textDown: '下拉刷新...',
        textUp: '松开刷新...',
        showTime: true
    }, function(ret, err){
        coversationList();
        api.refreshHeaderLoadDone();

    });

    //监听列表刷新
    api.addEventListener({
        name:'userInfoReload'
    },function(ret){
        window.location.reload();
    });
};


//打开会话页面
function openWin(targetId,name,conversationType,latestMessageId,user_img,sentTime) {
    api.sendEvent({
        name:'getHistoryMessages',
        extra:{
            targetId:targetId,
            conversationType:conversationType,
            latestMessageId:latestMessageId
        }
    });
    clearMessagesUnreadStatus(conversationType,targetId);
    if(conversationType=="PRIVATE"){
        api.openWin({
            name: "send_msg",
            url: 'send_msg_win.html',
            pageParam: {
               targetId:targetId,
               username:name,
               user_img:user_img,
               sentTime:sentTime,
               conversationType:conversationType
            },
        });
    }else{
        api.openWin({
            name: "send_groupmsg",
            url: 'send_groupmsg_win.html',
            pageParam: {
               targetId:targetId,
               username:name,
               sentTime:sentTime,
               conversationType:conversationType
            },
        });
    }

}

function getReload(){

  window.location.reload();
}

//时间差计算
function sj(sj) {
        var nowt = new Date().getTime();
        var a = new Date(parseInt(sj));
        var b = new Date(parseInt(nowt));
        var date1 = Date.parse(format(a, 4));
        var date2 = Date.parse(format(b, 4));
        var xxsj = Math.ceil((date2 - date1) / (60 * 1000))
        if (xxsj <= 1 && xxsj >= 0) {
                return "就刚才";
        } else if (xxsj <= 10 && xxsj > 1) {
                return xxsj + "分钟前";
        } else if (xxsj <= 60 && xxsj > 10) {
                return format(a, 1);
        } else if (xxsj <= 1440 && xxsj > 60) {
                return format(a, 1);
        } else if (xxsj <= 10080 && xxsj > 1440) {
                return format(a, 2);
        } else if (xxsj > 10080) {
                return format(a, 3);
        } else {
                return format(a, 3);
        }
}

//格式化时间
function format(now, type) {
        var show_day = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
        var year = now.getFullYear().toString();
        var month = (now.getMonth() + 1).toString();
        var day = (now.getDate()).toString();
        var tian = now.getDay().toString();
        var hour = (now.getHours()).toString();
        var minute = (now.getMinutes()).toString();
        var second = (now.getSeconds()).toString();
        if (hour.length == 1) {
                hour = "0" + hour;
        }
        if (minute.length == 1) {
                minute = "0" + minute;
        }
        if (second.length == 1) {
                second = "0" + second;
        }
        if (type == 1) {
                var dateTime = hour + ":" + minute;
        } else if (type == 2) {
                var dateTime = show_day[tian] + " " + hour + ":" + minute
        } else if (type == 3) {
                var dateTime = year + "-" + month + "-" + day
        } else if (type == 4) {
                var dateTime = year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second;
        } else if (type == 5) {
                var dateTime = show_day[tian];
        }
        return dateTime;
}

</script>
</html>
